package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.UserReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("user")
public class UserController {

    //注册
    //localhost:8080/user/register1?username=songge&password=niupi&age=30
    @RequestMapping("register1")
    public BaseRespVo register(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String age = request.getParameter("age");
        //如果需要Integer类型的age还需要自己做类型转换
        return BaseRespVo.ok();
    }

    //使用Integer直接接收到了请求参数：因为SpringMVC内置了转换器Converter
    //请求参数名和Handler方法的形参名一致
    //localhost:8080/user/register2?username=songge&password=niupi&age=30
    @RequestMapping("register2")
    //public BaseRespVo register2(String username, String password, String age) {
    public BaseRespVo register2(String username, String password, Integer age, int weight) {

        return BaseRespVo.ok(username + ":" + password + ";age=" + age + ";weight = " + weight);
    }

    //localhost:8080/user/register3?username=songge&password=niupi&age=30&weight=140
    //&hobbys=sing&hobbys=dance&hobbys=rap&hobbys=basketball
    @RequestMapping("register3")
    public BaseRespVo register3(String username, String password,
                                Integer age, int weight,
                                String[] hobbys) {

        return BaseRespVo.ok(hobbys);
    }

    //其实有提供对应的转换器，需要指定格式
    //localhost:8080/user/register4?username=songge&password=niupi&age=30&weight=140
    //&hobbys=sing&hobbys=dance&hobbys=rap&hobbys=basketball
    //&birthday=2020-02-21
    @RequestMapping("register4")
    public BaseRespVo register4(String username, String password,
                                Integer age, int weight,
                                String[] hobbys,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {

        return BaseRespVo.ok(birthday);
    }

    @Autowired
    ConfigurableConversionService conversionService;

    //自定义转换器：转换业务可以自己写，更灵活
    /**
     *   转换器什么时候生效:
     *   我们会去定义转换器的类型，
     *      当请求参数和Handler方法的形参名一致
     *      而类型刚好符合转换器的类型
     */
    //localhost:8080/user/register5?username=songge&password=niupi&age=30&weight=140
    //&birthday=2020-02-21
    @RequestMapping("register5")
    public BaseRespVo register5(String username, String password,
                                Integer age, int weight,
                                Date birthday) {

        return BaseRespVo.ok(birthday);
    }


    // localhost:8080/user/register6?username=songge&password=niupi&age=30&weight=140
    //&birthday=2020-02-21
    @RequestMapping("register6")
    public BaseRespVo register6(User user) {
        return BaseRespVo.ok(user);
    }

    @RequestMapping("list")
    //public BaseRespVo list(String username, String password, BaseParam param) {
    public BaseRespVo list(UserReqVo vo) {
        return BaseRespVo.ok();
    }

}
